package com.nju.edu.erp.service.Impl;


import com.nju.edu.erp.dao.ReceiptSheetDao;
import com.nju.edu.erp.enums.sheetState.PurchaseSheetState;
import com.nju.edu.erp.enums.sheetState.ReceiptSheetState;
import com.nju.edu.erp.model.po.*;
import com.nju.edu.erp.model.vo.ProductInfoVO;
import com.nju.edu.erp.model.vo.UserVO;
import com.nju.edu.erp.model.vo.purchase.PurchaseSheetContentVO;
import com.nju.edu.erp.model.vo.purchase.PurchaseSheetVO;
import com.nju.edu.erp.model.vo.receipt.ReceiptSheetVO;
import com.nju.edu.erp.model.vo.warehouse.WarehouseInputFormContentVO;
import com.nju.edu.erp.model.vo.warehouse.WarehouseInputFormVO;
import com.nju.edu.erp.service.finance.AccountService;
import com.nju.edu.erp.service.customer.CustomerService;
import com.nju.edu.erp.service.finance.FinanceReceiptService;
import com.nju.edu.erp.service.customer.CustomerService;
import com.nju.edu.erp.service.finance.AccountService;
import com.nju.edu.erp.utils.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class  FinanceReceiptServiceImp implements FinanceReceiptService {

    ReceiptSheetDao receiptSheetDao;
    CustomerService customerService;
    AccountService accountService;

    @Autowired
    public FinanceReceiptServiceImp(ReceiptSheetDao receiptSheetDao, CustomerService customerService,AccountService accountService){
        this.receiptSheetDao=receiptSheetDao;
        this.customerService=customerService;
        this.accountService=accountService;
    }

    @Override
    @Transactional
    public  void makeReceiptSheet(UserVO userVO, ReceiptSheetVO receiptSheetVO){

        ReceiptSheetPO receiptSheetPO = new ReceiptSheetPO();
        BeanUtils.copyProperties(receiptSheetVO, receiptSheetPO);
        // 此处根据制定单据人员确定操作员
        receiptSheetPO.setOperator(userVO.getName());
        ReceiptSheetPO latest = receiptSheetDao.getLatest();
        String id = IdGenerator.generateSheetId(latest == null ? null : latest.getId(), "SKD");
        receiptSheetPO.setId(id);
        receiptSheetPO.setState(ReceiptSheetState.PENDING);
        receiptSheetDao.save(receiptSheetPO);

    };

    @Override
    public List<ReceiptSheetVO> getReceiptSheetByState(ReceiptSheetState state){

        List<ReceiptSheetVO> res = new ArrayList<>();
        List<ReceiptSheetPO> all;
        if(state == null) {
            all = receiptSheetDao.findAll();
        } else {
            all = receiptSheetDao.findAllByState(state);
        }
        for(ReceiptSheetPO po: all) {
            ReceiptSheetVO vo = new ReceiptSheetVO();
            BeanUtils.copyProperties(po, vo);
            res.add(vo);
        }
        return res;
    };

    @Override
    @Transactional
    public void approval(String receiptSheetId, ReceiptSheetState state){
            // TODO 审批完成, 修改一系列状态
            // 更新客户表(更新应付字段)
            // 更新应付 payable
            receiptSheetDao.updateState(receiptSheetId,state);
            ReceiptSheetPO receiptSheet = receiptSheetDao.findOneById(receiptSheetId);
            CustomerPO customerPO = customerService.findCustomerByName(receiptSheet.getCustomer());
            customerPO.setPayable(customerPO.getPayable().subtract(receiptSheet.getTotalAmount()));
            customerService.updateCustomer(customerPO);

            AccountPO accountPO=accountService.findAccountByName(receiptSheet.getAccount());
            accountPO.setMoney(accountPO.getMoney().subtract(receiptSheet.getTotalAmount()));


    };



}
